/**
 * Private Variable Declaration
 * */
 import Modules.Account.ParentAccountDetail;
 import Modules.Account.ParentAccountInfo;
 import Modules.Windows.AdminWindow;
 
 import com.desidea.common.CommonClass;
 import com.desidea.components.grid.SuperGridEvent;
 import com.desidea.components.grid.button.SuperGridButton;
 import com.desidea.utils.DirtyChecker;
 import com.desidea.utils.IDirtyChecker;
 import com.desidea.webservice.PartnerService;
 import com.desidea.webservice.ProcessServiceRequestResultEvent;
 
 import flash.events.Event;
 import flash.events.MouseEvent;
 
 import mx.controls.Alert;
 import mx.controls.Button;
 import mx.controls.CheckBox;
 import mx.controls.dataGridClasses.DataGridColumn;
 import mx.rpc.events.FaultEvent;
 

/**
 * Event Handling - CreationComplete event
 * */
private function windowCreated():void{
	
	if(CommonClass.UserLoginInfo.CanCreateParentAccount){
		var newItemBtn:Button = new Button();
		newItemBtn.label = "Create New Parent Account";
		newItemBtn.addEventListener(MouseEvent.CLICK, function(event:Event):void{
															onButtonClick(event,"new");
														});
		parentAccountSummaryGrid.toolBar.addChild(newItemBtn);
		
	}
	this.parentAccountSummaryGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);

	loadData();
}

private function loadData():void{
	
	if( DirtyChecker.getInstance().isDirty){
		if (DirtyChecker.getInstance().sourceContainer is IDirtyChecker){
			(DirtyChecker.getInstance().sourceContainer as IDirtyChecker).promptToSave();
		}else{
			Alert.show("Save & Proceed");
		}
		return;
	}else{
		DirtyChecker.getInstance().resetDirtyChecker();
	}
	
	
	var params:String = "OPERATION=PARENTACCOUNT_LIST";
//						"&ISACTIVE=1";
//						"&USERID=1";
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForParentAccountsList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
//	BusySpinner.getInstance().play(Application.application.ModuleCvs);
}

private function callForParentAccountsList(event:ProcessServiceRequestResultEvent):void{
//	BusySpinner.getInstance().stop();	
	var response:XML = XML(event.result);
//	Alert.show(response.toString());
	lblError.text = "";
	if(response.RESPONSE.RESULT == "E"){
		lblError.text = response.MESSAGE
	}else if(response.RESPONSE.RESULT == 'S'){
		this.parentAccountSummaryGrid.dataProvider = response;
	}else{
		Alert.show("Unknow Error: Unable to load response");
	}
	setColumnWidths();
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
//	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}
private function onButtonClick(event:Event, identifier:String = ""):void{
	switch(identifier){
		case "new":
			displayParentAccountDetailPage();
			break;
	}
}

private function displayParentAccountDetailPage(info:ParentAccountInfo = null):void{
	if(info == null || info.id <= 0){
		var win:AdminWindow = this.parentDocument as AdminWindow;
		win.removeAllChildren();
		
		var detail:ParentAccountDetail = new ParentAccountDetail();
		detail.mode = "edit";
		win.addChild(detail);
		
	}else{
		//TODO: Display Account Details
	}
}


private function setColumnWidths():void{
	setColumnWidth(1, 80, "right");
	setColumnWidth(2, 200);
	setColumnWidth(3, 120);
	setColumnWidth(4, 100);
	setColumnWidth(5, 80);
	setColumnWidth(6, 80);
	setColumnWidth(7, 100);
	setColumnWidth(8, 80);
}

private function setColumnWidth(colIndex:int, colWidth:int, alignment:String = "left"):void{
	if(this.parentAccountSummaryGrid.GridComponent.columnCount > colIndex)	
		this.parentAccountSummaryGrid.GridComponent.columns[colIndex].width = colWidth;
		
		var col:DataGridColumn = this.parentAccountSummaryGrid.GridComponent.columns[colIndex];
		col.setStyle("textAlign", alignment);
}

private function gridButtonColumnClicked(event:SuperGridEvent):void{
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	
	if(mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		showDetail(mode, event.data.data["key"].toString());
	}else{
		Alert.show("Mode : " + mode + " not handled");
	}
	
}

private function showDetail(mode:String, key:String=""):void{
	var win:AdminWindow = this.parentDocument as AdminWindow;

	var pad:ParentAccountDetail = new ParentAccountDetail();
	pad.mode = win.data.toString().toLowerCase();
	pad.key = key;
	pad.mode = mode;
	win.removeAllChildren();
	win.addChild(pad);
}
